#include "timeobrowser.h"
#include "ui_timeobrowser.h"
#include <QWebFrame>
#include <QDebug>
#include <QRegExp>
#include "pref_window.h"

TimeoBrowser::TimeoBrowser(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TimeoBrowser)
{
    ui->setupUi(this);


//SIGNAUX/SLOT
    connect(ui->web,SIGNAL(loadFinished(bool)),this,SLOT(search_codeTimeo()));
    connect(this,SIGNAL(code_trouver()),this,SLOT(update_label()));
    connect(ui->btn,SIGNAL(rejected()),this,SLOT(close()));
    connect(ui->btn,SIGNAL(accepted()),parent,SLOT(Ajouter_Code()));


}

TimeoBrowser::~TimeoBrowser()
{
    delete ui;
}

void TimeoBrowser::search_codeTimeo()
{
    QWebFrame *page = ui->web->page()->currentFrame();
    //qDebug()<< "cherche" <<  page->toHtml();
    QRegExp reg_timeo;
    reg_timeo.setPattern("\\W{2}(\\d{2,3})</");
    reg_timeo.indexIn(page->toHtml());
    codeTimeo = reg_timeo.cap(1);

    //SI on trouve un code timéo avec une regexp dans la page html on emit un signal
    if (!codeTimeo.isEmpty())
    {
        emit code_trouver();
    }

}

void TimeoBrowser::update_label()
    {

        ui->codeTimeo_lbl->setText("Code Timeo : <b>"+codeTimeo+"</b>");
    }


void TimeoBrowser::Termine()
{

    //qDebug()<< "code_timeo_browser = "<< codeTimeo;
    emit iFinished(codeTimeo);


}


